/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jforum.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import jforum.domain.Ban;
import jforum.domain.User;
import jforum.util.DBConnector;

/**
 *
 * @author Вадим
 */
public class BanDAO implements BasicDAO<Ban> {
    private DBConnector connector;

    public BanDAO(DBConnector conn) {
        this.connector=conn;
    }

    private Ban getFromResultSet(ResultSet rs) throws SQLException {
        User user=new User(rs.getInt("user_id"),rs.getString("login"));
        return new Ban(rs.getInt("ban_id"), user, rs.getLong("start_time"), rs.getLong("end_time"));
    }

    public Ban Load(int id) throws SQLException {
        Ban b=null;
        ResultSet rs=this.connector.executePreparedQuery("SELECT * FROM \"ban\" b, \"user\" u "+
                "WHERE b.user_id=u.user_id AND b.ban_id=?;", id);
        if (rs.next())
        {
            b=this.getFromResultSet(rs);
        }
        rs.close();
        return b;
    }

    public Ban LoadOrCreate(int id) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Ban> LoadList() throws SQLException {
        List<Ban> list = new LinkedList<Ban>();
        ResultSet rs=this.connector.executeQuery("SELECT * FROM \"ban\" b , \"user\" u WHERE b.user_id=u.user_id;");
        while (rs.next())
        {
            Ban b = this.getFromResultSet(rs);
            list.add(b);
        }
        rs.close();
        return list;
    }

    public List<Ban> LoadPage(int startRowIndex, int maximumRows) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void Save(Ban obj) throws SQLException {
        this.connector.executePreparedUpdate("INSERT INTO \"ban\""+
                " (user_id, start_time, end_time) VALUES (?, ?, ?);", obj);
    }

    public void Update(Ban obj) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void Delete(Ban obj) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void DeleteAll() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int GetCount() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
